package b1.scenes.crime
{
	import b1.conifg.VirtualPlayerConfig;
	import b1.controls.tab.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.detect.*;
	import b1.scenes.home.*;
	import b1.scenes.home.police.*;
	import b1.scenes.home.wanted.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.filters.*;
	import flash.text.*;
	import flash.media.Sound;

	public class CrimeDialog extends TabController
	{
		private static var instance:CrimeDialog;
		
		public function CrimeDialog()
		{
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("TabControllerBackground");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("TabTitleBackground");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton1");
			super(TabControllerBackground, CloseButton);
			var tab:Tab = new Tab("邻居", 4, 2, 5, 3);
			this.addTab(TabTitleBackground, tab);
			tab = new Tab("好友", 4, 2, 5, 3);
			this.addTab(TabTitleBackground, tab);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	titleText.width = 45;
        	titleText.wordWrap = true;
        	titleText.filters = [BFont.greenFontGlowFilter];
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 20, 0x000000, true, true);
			titleText.text = "请 选 择 作 案 的 对 象 ？ ";
			titleText.x = 80;
			titleText.y = 60;
			addChild(titleText);
		}
		
		public function refresh():void
		{
			for (var i:int = 0; i < this.tabs.length; i++)
			{
				(this.tabs[i] as Tab).removeAll();
			}
			var players:Object = PlayerInfoHelper.getInstance().players;
			var playersId:Array = new Array();
			for (var playerId:String in players)
			{
				playersId.push(parseInt(playerId));
			}
			playersId.sort(Array.NUMERIC);
			for (i = 0; i < playersId.length; i++)
			{
				var player:PlayerInfo = players[playersId[i]];
				if (player.type != 0)
				{
					var crimeItem:CrimeItem
					//实际用户（邻居或者好友）
					if (player.type > 0)
					{
						if (player.name != "")
						{
							crimeItem = new CrimeItem(player.id);
							(this.tabs[player.type - 1] as Tab).addItem(crimeItem, 125, 125);
						}
					}
					//虚拟用户
					else if (player is VirtualPlayerConfig)
					{
						if ((player as VirtualPlayerConfig).appearInCrimeFlag)
						{
							crimeItem = new CrimeItem(player.id);
							(this.tabs[0] as Tab).addItem(crimeItem, 125, 125);
						}
					}
				}
			}
			
		}

		public static function getInstance():CrimeDialog
		{
			if (!instance)
				instance = new CrimeDialog();
			return instance;
		}
		
		public function open(parentScene:Sprite):void
		{
			refresh();
			if (!parentScene.contains(this))
			{
				this.x = (BFont.stageWidth - this.width) / 2;
				this.y = 40;
				parentScene.addChild(this);
				//播放音效
				var SoundButton1:Class = ResourceHelper.getInstance().getClassByName("SoundButton1");
				var sound:Sound = new SoundButton1();
				sound.play(); 
			}
			WantedTab.getInstance().close();
			Store.getInstance().close();
			DetectDiaglog.getInstance().close();
			PoliceStation.getInstance().close();
			BottomNavigator.getInstance().hide();
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
	}
}